package tp.pr4.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import tp.pr4.RobotEngine;
import tp.pr4.Rotation;
import tp.pr4.instructions.MoveInstruction;
import tp.pr4.instructions.RedoInstruction;
import tp.pr4.instructions.TurnInstruction;
import tp.pr4.instructions.UndoInstruction;
import tp.pr4.listeners.AbstractRobotListener;
import tp.pr4.sound.Sound;

public class MainWindow extends JFrame{
	private static final long serialVersionUID = 1L;
	private int width = 900;
	private int height = 600;
	
	private RobotEngine r;
	
	
	/**
	 * Creates the window and the panels using Swing Components.
	 * @param r
	 */
	public MainWindow (final RobotEngine r) {
		super("WALL-E: The game");
		this.r = r;
		this.setLayout(new BorderLayout());
		this.add(new RobotPanel(r), BorderLayout.NORTH);
		this.add(new NavigationPanel(r), BorderLayout.CENTER);
		this.setJMenuBar(new TopMenuBar(r));
		
		//una vez hemos añadido todo forzamos una actualización para que salga
		r.getNavigationModule().forceUpdate();
		
		this.addKeyController();
		this.setParams();
		
		//vamos a reproducir el de inicio una vez y el principal indefinidamente
		Sound[] ss = {Sound.gameMusicIntro, Sound.gameMusicLoop};
		//lo indicamos aquí
		int[] st = {Sound.LOOP_ONCE,Sound.LOOP_CONTINUOUSLY};
		//y a bailar!
		Sound.playSounds(ss, st);
		
		final JFrame frame = this;
		
		r.addRobotListener(new AbstractRobotListener() {
			@Override
			public void onDeath() {
				//JOptionPane.showMessageDialog(frame, "Pulsa aceptar para perder", "Eres malísimo", JOptionPane.INFORMATION_MESSAGE);
				while (JOptionPane.showConfirmDialog(frame, "Pulsa sí para perder", "Eres malísimo", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {}
				System.exit(0);
			}
			
		});
	}
	
	/**
	 * Sets various parameters to default values
	 */
	private void setParams() {
		this.setSize(this.width, this.height);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setMinimumSize(new Dimension(600,300));
	}
	
	/**
	 * Adds a key controller to the window. It will catch any key events when the window has the OS focus. Any unprocessed events will continue down the priority line thus not being consumed.
	 */
	private void addKeyController() {
		//la basura del addKeyListener no va porque el control se pira hacia los componentes de dentro
		KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
			@Override
			public boolean dispatchKeyEvent(KeyEvent arg0) {
				if (arg0.getID() == KeyEvent.KEY_PRESSED) {
					if (arg0.getKeyCode() == KeyEvent.VK_UP)
						r.communicateRobot(new MoveInstruction());
					else if (arg0.getKeyCode() == KeyEvent.VK_LEFT)
						r.communicateRobot(new TurnInstruction(Rotation.LEFT));
					else if (arg0.getKeyCode() == KeyEvent.VK_RIGHT)
						r.communicateRobot(new TurnInstruction(Rotation.RIGHT));
					else if (arg0.isControlDown() && arg0.getKeyCode() == KeyEvent.VK_Z )
						r.communicateRobot(new UndoInstruction());
					else if (arg0.isControlDown() && arg0.getKeyCode() == KeyEvent.VK_Y )
						r.communicateRobot(new RedoInstruction());
					else return false;
					arg0.consume();
				}
				return false;
			}
		});
	}

}
